﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.IO;


namespace ClientAplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private ClientAplication.serviceNapBai.Service1 ws;
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            ws = new ClientAplication.serviceNapBai.Service1();
            string mm=TimMaMay();
            DataTable dsmaydk = ws.Ds_MayDangKi();
            dtgv_SV.DataSource = dsmaydk;
            dtgv_DsNapbai.DataSource = ws.Ds_NapBai();
            if (!ws.Kiemtra_DangKiTV(mm))
            {
                txt_mssv.Enabled = false;
                txt_ten.Enabled = false;
                bnt_DK.Enabled = false;
                btn_chonfile.Enabled = true;
            }
        }
        private string TimMaMay()
        {
            string mac = "";
            var card = NetworkInterface.GetAllNetworkInterfaces().Where(x => x.OperationalStatus == OperationalStatus.Up);
            foreach (NetworkInterface nic in card)
            {
                PhysicalAddress MAC = nic.GetPhysicalAddress();
                mac = MAC.ToString();
                if (mac != null) break;
            }
            return mac;
        }
        private void btnDK_Click(object sender, EventArgs e)
        {
            ws = new ClientAplication.serviceNapBai.Service1();
            string mac = TimMaMay();          
            
            int mssv = int.Parse(txt_mssv.Text);
            DateTime dt = DateTime.Now;
            if (ws.DangKi(mac, txt_ten.Text, mssv,dt) == 1)
            {
                MessageBox.Show("Đăng kí thành công!");
                txt_mssv.Text = txt_mssv.Text;
                txt_mssv.Text = txt_ten.Text;
                btn_chonfile.Enabled = true;
                txt_mssv.Enabled = false;
                txt_ten.Enabled = false;
                bnt_DK.Enabled = false;
                LoadData();
            }
            else
            {
                MessageBox.Show("Có thể máy này đã đăng kí!");
            }
        }


        private void btn_Openfile_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txt_path.Text = openFileDialog1.FileName;
            }
        }
        private void UploadFile(string filename)
        {
            try
            {
                //lay duong dan cua file
                String strFile = System.IO.Path.GetFileName(filename);
                //Lay thong tin file chon 
                FileInfo fInfo = new FileInfo(filename);

                // Lay do dai goi tin
                //yeu cau goi tin la 4MB
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                
                if (dLen < 4)
                {
                    // 
                    // thuc hien chuyen goi file da chon san kieu nhi phan
                    FileStream fStream = new FileStream(filename,
                    FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    //Doc cac byte vao mang data
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    //Thuc hien viec upload file len server
                    string sTmp = ws.UploadFile(data, strFile);                     
                    fStream.Close();
                    fStream.Dispose();                    
                    //Neu up load thanh cong
                    MessageBox.Show("File Upload: " + sTmp);
                    if (sTmp == "OK")
                    {
                        ws.CapNhatNapBai(TimMaMay(), DateTime.Now, filename);
                    }
                }
                else
                {
                    // file qua kich thuoc quy dinh
                    MessageBox.Show("Kích thước file quá lớn");
                }
            }
            catch (Exception ex)
            {
                // Loi load file
                MessageBox.Show(ex.Message.ToString(), "Lỗi Upload");
            }
        }
        private string MacID = "";
        private void btn_nopbai_Click_1(object sender, EventArgs e)
        {
            ws = new ClientAplication.serviceNapBai.Service1();
            string mac = TimMaMay();            
            if (ws.Kiemtra_NapBai(mac))
            {
                if (txt_path.Text != string.Empty)
                {
                    UploadFile(txt_path.Text);
                    LoadData();
                }
                else
                {
                    MessageBox.Show("Bạn phải chọn đúng file!");
                }
            }
            else
            {
                MessageBox.Show("Máy này đã gửi bài!");
            }
        }
    }
}
